<template>
  <div class="login-container">
    <el-card class="login-card">
      <h1 class="login-title">系统登录</h1>
      <div class="login-form">
        <el-form :mode="loginForm" @submit.prevent="handleLogin" :rules="rules">
          <el-row>
            <el-col :span="24">
              <el-form-item label="用户名" label-width="60px">
                <el-input v-model="loginForm.username" autocomplete="off" prefix-icon="user" maxlength="10"
                          show-word-limit/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="密码" label-width="60px">
                <el-input v-model="loginForm.password" type="password" autocomplete="off" prefix-icon="lock"
                          show-password/>
              </el-form-item>
            </el-col>
          </el-row>
       
          <el-form-item>
            <el-button type="primary" @click="handleLogin" style="width: 100%;">登录</el-button>
          </el-form-item>
        </el-form>

      </div>

      <!-- 无帐号，点此注册 -->
      <el-row style="text-align: right;">
        <el-col>
          <el-text class="w-150px mb-2">
            若无帐号，点此>>
            <RouterLink to="/register" style="font-size: 1.2em;">注册</RouterLink>
          </el-text>
        </el-col>
      </el-row>
    </el-card>
  </div>

</template>

<script setup>
import {ref} from 'vue';
import {RouterLink} from 'vue-router'
import {useRouter} from 'vue-router'
import { loginOn} from '../api/index'
import { ElMessage, ElMessageBox } from 'element-plus';
import { useMenusStore } from '@/stores/Menu'
const menusStore = useMenusStore()

const loginForm = ref({
  username: 'admin',
  password: '123456',
});

const router = useRouter()

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}
 async  function handleLogin() {
  console.log('登录信息:', loginForm.value);
  //todo 这里可以添加登录逻辑
  await  loginOn(loginForm.value).then( async (res)=>{
    console.log(res, '登录返回<<<<')
    let data = res.data
    let token = res.headers.authorization
    localStorage.setItem("token",token)
     //添加动态路由
     await menusStore.setRoutes()

    if(data.status=='200'){
      // 跳转到home页面
      router.push("/system/paramInfo")
    } else {//登录失败
      ElMessage.error(data.message)
      return false;
    }
    
  }).catch(err=>{
    console.log('login error>>>',err)
  })
   
  

}

</script>
<style scoped>

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/loginbg-1.png'); /* 假设背景图片在assets目录下 */
  background-size: cover;
  background-position: center;
}

.login-card {
  width: 420px;
  padding: 20px;
  /* background-color: #fafafa; */
  background-color: #fefefe;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.login-form {
  width: 100%;
}

.captcha-image {
  cursor: pointer; /* 当鼠标悬停时显示为可点击状态 */
  margin-left: 10px; /* 与输入框保持一定间距 */
  height: 36px; /* 控制图片高度，根据实际情况调整 */
  vertical-align: middle; /* 垂直居中对齐 */
}
</style>